/*
 * Port.c
 *
 * Created: 11/13/2012 12:16:15 AM
 *  Author: Hp-Minion
 */ 
/*****************************************************************************************/
#include <avr/io.h>
#include "General.h"
#include "Port.h"

/*****************************************************************************************/
void PinMode(unsigned int nPin, char nType, enum enInOut nDirection)
{
	switch (nPin)
	{
		case 0: 
		{
			switch (nType)
			{
				case 'D': 
				    switch (nDirection)
	                {
                        case INPUT:
						    DDRD &= ~(1<<DDD0);
							break;
									
						case OUTPUT:
						    DDRD |= (1<<DDD0);
							break;
									
						default: 
							break;
					}
					break;
						
				case 'A':
				    switch (nDirection)
					{
						case INPUT:
						    DDRC &= ~(1<<DDC0);
							break;
									
						case OUTPUT:
						    DDRC |= (1<<DDC0);
							break;
									
						default: 
							break;
					}
					break;
						
				default: 
					break;
		    }
		}
		break;
		
		case 1:
		{
			switch (nType)
			{
				case 'D': 
				    switch (nDirection)
					{
						case INPUT:
						    DDRD &= ~(1<<DDD1);
							break;
									
						case OUTPUT:
						    DDRD |= (1<<DDD1);
							break;
									
						default: 
							break;
					}
					break;
						
				case 'A':
				    switch (nDirection)
					{
						case INPUT:
						    DDRC &= ~(1<<DDC1);
							break;
									
						case OUTPUT:
						    DDRC |= (1<<DDC1);
							break;
									
						default: 
						break;
					}
				    break;
						
				default: 
				    break;
			}
		}
		break;
		
		case 2: {
					switch (nType)
					{
						case 'D': switch (nDirection)
								{
									case INPUT:		DDRD &= ~(1<<DDD2);
									break;
									
									case OUTPUT:	DDRD |= (1<<DDD2);
									break;
									
									default: 
									break;
								}
						break;
						
						case 'A':	switch (nDirection)
								{
									case INPUT:		DDRC &= ~(1<<DDC2);
									break;
										
									case OUTPUT:	DDRC |= (1<<DDC2);
									break;
										
									default: 
									break;
								}
						break;
						
						default: 
						break;
					}
				}
		break;
		
		case 3: {
					switch (nType)
					{
						case 'D': switch (nDirection)
								{
									case INPUT:		DDRD &= ~(1<<DDD3);
									break;
										
									case OUTPUT:	DDRD |= (1<<DDD3);
									break;
									
									default: 
									break;
								}
						break;
						
						case 'A':	switch (nDirection)
								{
									case INPUT:		DDRC &= ~(1<<DDC3);
									break;
									
									case OUTPUT:	DDRC |= (1<<DDC3);
									break;
									
									default: 
									break;
								}
						break;
						
						default: 
						break;
					}
				}
		break;
		
		case 4: {
					switch (nType)
					{
						case 'D': switch (nDirection)
								{
									case INPUT:		DDRD &= ~(1<<DDD4);
									break;
									
									case OUTPUT:	DDRD |= (1<<DDD4);
									break;
									
									default: 
									break;
								}
						break;
						
						case 'A':	switch (nDirection)
								{
									case INPUT:		DDRC &= ~(1<<DDC4);
									break;
									
									case OUTPUT:	DDRC |= (1<<DDC4);
									break;
									
									default: 
									break;
								}
						break;
							
						default: 
						break;
					}
				}
		break;
			
		case 5: {
					switch (nType)
					{
						case 'D': switch (nDirection)
								{
									case INPUT:		DDRD &= ~(1<<DDD5);
									break;
									
									case OUTPUT:	DDRD |= (1<<DDD5);
									break;
									
									default: 
									break;
								}
						break;
						
						case 'A':	switch (nDirection)
								{
									case INPUT:		DDRC &= ~(1<<DDC5);
									break;
									
									case OUTPUT:	DDRC |= (1<<DDC5);
									break;
									
									default: 
									break;
								}
						break;
						
						default: 
						break;
					}
				}
		break;
		
		case 6: {
					switch (nDirection)
					{
						case INPUT:		DDRD &= ~(1<<DDD6);
						break;
						
						case OUTPUT:	DDRD |= (1<<DDD6);
						break;
						
						default: 
						break;
					}
				}
		break;
		
		case 7: {
					switch (nDirection)
					{
						case INPUT:		DDRD &= ~(1<<DDD7);
						break;
						
						case OUTPUT:	DDRD |= (1<<DDD7);
						break;
						
						default: 
						break;
					}
				}
		break;
		
		case 8: {
					switch (nDirection)
					{
						case INPUT:		DDRB &= ~(1<<DDB0);
						break;
						
						case OUTPUT:	DDRB |= (1<<DDB0);
						break;
						
						default: 
						break;
					}
				}
		break;
		
		case 9: {
					switch (nDirection)
					{
						case INPUT:		DDRB &= ~(1<<DDB1);
						break;
						
						case OUTPUT:	DDRB |= (1<<DDB1);
						break;
						
						default: 
						break;
					}
				}
		break;
		
		case 10: {
					switch (nDirection)
					{
						case INPUT:		DDRB &= ~(1<<DDB2);
						break;
						
						case OUTPUT:	DDRB |= (1<<DDB2);
						break;
						
						default: 
						break;
					}
				}
		break;
			
		case 11: {
					switch (nDirection)
					{
						case INPUT:		DDRB &= ~(1<<DDB3);
						break;
						
						case OUTPUT:	DDRB |= (1<<DDB3);
						break;
						
						default: 
						break;
					}
				}
		break;
		
		case 12: {
					switch (nDirection)
					{
						case INPUT:		DDRB &= ~(1<<DDB4);
						break;
						
						case OUTPUT:	DDRB |= (1<<DDB4);
						break;
						
						default: 
						break;
					}
				}
		break;
		
		case 13: {
					switch (nDirection)
					{
						case INPUT:		DDRB &= ~(1<<DDB5);
						break;
						
						case OUTPUT:	DDRB |= (1<<DDB5);
						break;
							
						default: 
						break;
					}
				}
		break;	
		
		default: 
		break;		
	}	
}

/*****************************************************************************************/
void DigitalWrite(unsigned int nPin, enum enLevel nValue)
{
	switch (nPin)
	{
		case 0: 
		{
			switch (nValue)
			{
				case LOW:
					PORTD &= ~(1 << PD0);
				break;
						
				case HIGH:
					PORTD |= (1 << PD0);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 1: 
		{
			switch (nValue)
			{
				case LOW:
					PORTD &= ~(1 << PD1);
				break;
						
				case HIGH:
					PORTD |= (1 << PD1);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 2:
		{
			switch (nValue)
			{
				case LOW:
					PORTD &= ~(1 << PD2);
				break;
						
				case HIGH:
					PORTD |= (1 << PD2);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 3:
		{
			switch (nValue)
			{
				case LOW:
					PORTD &= ~(1 << PD3);
				break;
						
				case HIGH:
					PORTD |= (1 << PD3);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 4:
		{
			switch (nValue)
			{
				case LOW:
					PORTD &= ~(1 << PD4);
				break;
						
				case HIGH:
					PORTD |= (1 << PD4);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 5:
		{
			switch (nValue)
			{
				case LOW:
					PORTD &= ~(1 << PD5);
				break;
						
				case HIGH:
					PORTD |= (1 << PD5);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 6:
		{
			switch (nValue)
			{
				case LOW:
					PORTD &= ~(1 << PD6);
				break;
						
				case HIGH:
					PORTD |= (1 << PD6);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 7:
		{
			switch (nValue)
			{
				case LOW:
					PORTD &= ~(1 << PD7);
				break;
						
				case HIGH:
					PORTD |= (1 << PD7);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 8:
		{
			switch (nValue)
				{
					case LOW:
						PORTB &= ~(1 << PB0);
					break;
						
					case HIGH:
						PORTB |= (1 << PB0);
					break;
						
					default:
					break;
				}
		}
		break;
		
		case 9:
		{
			switch (nValue)
			{
				case LOW:
					PORTB &= ~(1 << PB1);
				break;
						
				case HIGH:
					PORTB |= (1 << PB1);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 10:
		{
			switch (nValue)
			{
				case LOW:
					PORTB &= ~(1 << PB2);
				break;
						
				case HIGH:
					PORTB |= (1 << PB2);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 11:
		{
			switch (nValue)
			{
				case LOW:
					PORTB &= ~(1 << PB3);
				break;
						
				case HIGH:
					PORTB |= (1 << PB3);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 12:
		{
			switch (nValue)
			{
				case LOW:
					PORTB &= ~(1 << PB4);
				break;
						
				case HIGH:
					PORTB |= (1 << PB4);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 13:
		{
			switch (nValue)
			{
				case LOW:
					PORTB &= ~(1 << PB5);
				break;
						
				case HIGH:
					PORTB |= (1 << PB5);
				break;
						
				default:
				break;
			}
		}
		break;
		
		default:
		break;
	}
		
}

/*****************************************************************************************/
unsigned int DigitalRead(unsigned int nPin)
{
	unsigned int state = 0;
	unsigned int flag = 0;
	switch (nPin)
	{
		case 0: {
					flag = (PIND & (1<<PD0));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 1: {
					flag = (PIND & (1<<PD1));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 2: {
					flag = (PIND & (1<<PD2));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 3: {
					flag = (PIND & (1<<PD3));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 4: {
					flag = (PIND & (1<<PD4));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 5: {
					flag = (PIND & (1<<PD5));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 6: {
					flag = (PIND & (1<<PD6));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 7: {
					flag = (PIND & (1<<PD7));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 8: {
					flag = (PINB & (1<<PB0));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 9: {
					flag = (PINB & (1<<PB1));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 10: {
					flag = (PINB & (1<<PB2));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 11: {
					flag = (PINB & (1<<PB3));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 12: {
					flag = (PINB & (1<<PB4));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 13: {
					flag = (PINB & (1<<PB5));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		default:
		break;
	}
	return state;	
}

/*****************************************************************************************/
void AnalogWrite(unsigned int nPin, enum enLevel nValue)
{
	switch (nPin)
	{
		case 0: 
		{
			switch (nValue)
			{
				case LOW:
					PORTC &= ~(1 << PC0);
				break;
						
				case HIGH:
					PORTC |= (1 << PC0);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 1: 
		{
			switch (nValue)
			{
				case LOW:
					PORTC &= ~(1 << PC1);
				break;
						
				case HIGH:
					PORTC |= (1 << PC1);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 2:
		{
			switch (nValue)
			{
				case LOW:
					PORTC &= ~(1 << PC2);
				break;
						
				case HIGH:
					PORTC |= (1 << PC2);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 3:
		{
			switch (nValue)
			{
				case LOW:
					PORTC &= ~(1 << PC3);
				break;
						
				case HIGH:
					PORTC |= (1 << PC3);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 4:
		{
			switch (nValue)
			{
				case LOW:
					PORTC &= ~(1 << PC4);
				break;
						
				case HIGH:
					PORTC |= (1 << PC4);
				break;
						
				default:
				break;
			}
		}
		break;
		
		case 5:
		{
			switch (nValue)
			{
				case LOW:
					PORTC &= ~(1 << PC5);
				break;
						
				case HIGH:
					PORTC |= (1 << PC5);
				break;
						
				default:
				break;
			}
		}
		break;
		
		default:
		break;
	}
}		

/*****************************************************************************************/
unsigned int AnalogRead (unsigned int Pin)
{
	unsigned int state = 0;
	unsigned int flag = 0;
	switch (Pin)
	{
		case 0: {
					flag = (PINC & (1<<PC0));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 1: {
					flag = (PINC & (1<<PC1));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 2: {
					flag = (PINC & (1<<PC2));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 3: {
					flag = (PINC & (1<<PC3));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 4: {
					flag = (PINC & (1<<PC4));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		case 5: {
					flag = (PINC & (1<<PC5));
					if (flag == 0)
						{
							state = 0;
						}
					else
						{
							state = 1;
						}
				}
		break;
		
		default:
		break;
	}
	return state;		
	
} 

/*****************************************************************************************/
unsigned long map(unsigned long x, unsigned long in_min, unsigned long in_max, unsigned long out_min, unsigned long out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/*****************************************************************************************/
unsigned int FromASCII(unsigned char ascii)
{
	unsigned int dec = 0;
	
	if ((ascii <= 57) && (ascii >= 48))
	{
		switch(ascii)
		{
			case 48: dec = 0;
			break;
			case 49: dec = 1;
			break;
			case 50: dec = 2;
			break;
			case 51: dec = 3;
			break;
			case 52: dec = 4;
			break;
			case 53: dec = 5;
			break;
			case 54: dec = 6;
			break;
			case 55: dec = 7;
			break;
			case 56: dec = 8;
			break;
			case 57: dec = 9;
			break;
		}
	}
	else
		dec = 0;
	return dec; 
}
/*****************************************************************************************/
